import java.util.Random;

/**
 * 加减法口算练习系统-v0.2
 * 故事二：100以上的数和负数
 * 新增功能：
 * 1. 每个算式还需要答案
 * 2. 加法算式的和不能超过100
 * 3. 减法算式的差不能小于0
 * 4. 每行整齐地多显示几个算式
 */
public class CalculationSystemV02 {
    private Random random;

    public CalculationSystemV02() {
        this.random = new Random();
    }

    /**
     * 生成指定范围内的随机数
     * @param min 最小值（包含）
     * @param max 最大值（不包含）
     * @return 随机整数
     */
    private int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min) + min;
    }

    /**
     * 算式数据结构：使用内部类表示算式
     */
    public static class Equation {
        int num1;
        int num2;
        boolean isAddition;
        int answer;
        String expression;

        public Equation(int num1, int num2, boolean isAddition) {
            this.num1 = num1;
            this.num2 = num2;
            this.isAddition = isAddition;

            if (isAddition) {
                this.answer = num1 + num2;
                this.expression = num1 + " + " + num2 + " = " + answer;
            } else {
                this.answer = num1 - num2;
                this.expression = num1 + " - " + num2 + " = " + answer;
            }
        }

        @Override
        public String toString() {
            return expression;
        }
    }

    /**
     * 生成一道加法算式（和不超过100）
     * @return 算式对象
     */
    private Equation generateAdditionEquation() {
        // 确保和不超过100
        int num1 = generateRandomNumber(0, 101); // 0-100
        int maxNum2 = 100 - num1; // 确保 num1 + num2 <= 100
        int num2 = generateRandomNumber(0, maxNum2 + 1);

        return new Equation(num1, num2, true);
    }

    /**
     * 生成一道减法算式（差不小于0）
     * @return 算式对象
     */
    private Equation generateSubtractionEquation() {
        // 确保差不小于0（即 num1 >= num2）
        int num1 = generateRandomNumber(0, 101); // 0-100
        int num2 = generateRandomNumber(0, num1 + 1); // 0到num1之间，确保 num1 - num2 >= 0

        return new Equation(num1, num2, false);
    }

    /**
     * 生成50道习题
     */
    public void generateExercises() {
        System.out.println("加减法口算练习系统-v0.2");
        System.out.println("========================");
        System.out.println("生成50道100以内加减法算式（带答案）：\n");

        Equation[] equations = new Equation[50];

        // 生成50道算式
        for (int i = 0; i < 50; i++) {
            boolean isAddition = random.nextBoolean();
            if (isAddition) {
                equations[i] = generateAdditionEquation();
            } else {
                equations[i] = generateSubtractionEquation();
            }
        }

        // 每行显示5个算式，整齐排列
        int equationsPerLine = 5;
        for (int i = 0; i < equations.length; i++) {
            System.out.printf("%2d. %-20s", i + 1, equations[i].toString());

            // 每行显示5个算式后换行
            if ((i + 1) % equationsPerLine == 0) {
                System.out.println();
            }
        }

        // 如果最后一行不满5个，也要换行
        if (equations.length % equationsPerLine != 0) {
            System.out.println();
        }
    }

    public static void main(String[] args) {
        CalculationSystemV02 system = new CalculationSystemV02();
        system.generateExercises();
    }
}
